package com.company.cloud.thirduser.moudules.api.impl;

import cn.hutool.json.JSONUtil;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.thirduser.moudules.api.SsoSaasApi;
import com.company.cloud.thirduser.moudules.dto.AccountInfoDto;
import com.company.cloud.thirduser.moudules.dto.SsoAccountInfoDto;
import com.company.cloud.thirduser.moudules.dto.requestDto.SsoAccountInfoRequestDto;
import com.company.cloud.thirduser.moudules.dto.responseDto.SsoAccountInfoResponseDto;
import com.company.cloud.thirduser.moudules.properties.SsoSaasProperties;
import com.company.cloud.thirduser.moudules.utils.SsoSaasUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SsoSaasServiceImpl implements SsoSaasApi {

    @Autowired
    SsoSaasProperties ssoSaasProperties;

    @Autowired
    SsoSaasUtil ssoSaasUtil;

    @Override
    public List<SsoAccountInfoDto> sereachAccount(AccountInfoDto accountInfoDto){
       String response =  ssoSaasUtil.sereachAccount(setRequestBody(accountInfoDto));
        SsoAccountInfoResponseDto responseDto = JSONUtil.toBean(response,SsoAccountInfoResponseDto.class);
        if ("0".equals(responseDto.getReturnCode())){
            return responseDto.getAccountInfoList();
        }else {
            return null;
        }
    }

    @Override
    public List<SsoAccountInfoDto> addAccount(AccountInfoDto accountInfoDto){
        if (!StringUtils.hasText(accountInfoDto.getPassword())){
            accountInfoDto.setPassword(accountInfoDto.getPhone());
        }
        String response = ssoSaasUtil.addAccount(setRequestBody(accountInfoDto));
        SsoAccountInfoResponseDto responseDto = JSONUtil.toBean(response,SsoAccountInfoResponseDto.class);
        if ("0".equals(responseDto.getReturnCode())){
            return responseDto.getAccountInfoList();
        }else {
            return null;
        }
    }

    @Override
    public BaseResult modifyAccount(AccountInfoDto accountInfoDto){
        String response = ssoSaasUtil.midifyAccount(setRequestBody(accountInfoDto));
        SsoAccountInfoResponseDto responseDto = JSONUtil.toBean(response,SsoAccountInfoResponseDto.class);
        if ("0".equals(responseDto.getReturnCode())){
            return BaseResult.ok(responseDto.getReturnMessage());
        }else {
            return BaseResult.error(responseDto.getReturnMessage());
        }
    }
    private SsoAccountInfoRequestDto setRequestBody(AccountInfoDto accountInfoDto){
        SsoAccountInfoRequestDto requestBody = new SsoAccountInfoRequestDto();
        SsoAccountInfoDto accountInfo = new SsoAccountInfoDto();
        accountInfo.setSource(ssoSaasProperties.getSourctTarget());
        accountInfo.setOwnerSystem("1");
        accountInfo.setPhone(accountInfoDto.getPhone());
        if (StringUtils.hasText(accountInfoDto.getPassword())){
            accountInfo.setPassword(DigestUtils.md5Hex(accountInfoDto.getPassword()));
        }
        if (StringUtils.hasText(accountInfoDto.getUserId())){
            accountInfo.setUserId(accountInfoDto.getUserId());
        }
        requestBody.setAccountInfo(accountInfo);
        return requestBody;
    }
}
